package com.ylsf.blog.service.impl;

import com.ylsf.blog.mapper.CommentMapper;
import com.ylsf.blog.pojo.Comment;
import com.ylsf.blog.service.SendCommentService;
import com.ylsf.blog.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;

/**
 * 发送评论服务实现类
 */
public class SendCommentServiceImpl implements SendCommentService {
    @Override
    public Integer sendComment(String author, String commentText, Date dateTime, Integer articleId) {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CommentMapper commentMapper =sqlSession.getMapper(CommentMapper.class);
        Integer integer=0;
        try {
            integer = commentMapper.insertComment(new Comment(null, articleId, author, commentText, dateTime));
            sqlSession.commit();
        }catch (Exception e) {
            if (sqlSession != null) {
                sqlSession.rollback(); // 在catch块中进行回滚操作
            }
        }finally {
            if (sqlSession != null) {
                SqlSessionUtil.close(sqlSession); // 在finally块中关闭SqlSession
            }
        }
        return integer;
    }
}
